package com.qms.qep.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qms.common.utils.DateUtils;
import com.qms.qep.domain.Batch;
import com.qms.qep.domain.vo.BatchTraceVO;
import com.qms.qep.domain.vo.BatchTraceVO.CertificateSummary;
import com.qms.qep.domain.vo.BatchTraceVO.InspectionSummary;
import com.qms.qep.mapper.BatchMapper;
import com.qms.qep.mapper.ProcessRecordMapper;
import com.qms.qep.service.IBatchService;

/**
 * 批次信息Service业务层处理
 * 
 * @author qep
 */
@Service
public class BatchServiceImpl implements IBatchService 
{
    @Autowired
    private BatchMapper batchMapper;
    
    @Autowired
    private ProcessRecordMapper processRecordMapper;

    /**
     * 查询批次信息
     * 
     * @param id 批次信息主键
     * @return 批次信息
     */
    @Override
    public Batch selectBatchById(Long id)
    {
        return batchMapper.selectBatchById(id);
    }
    
    /**
     * 根据批次号查询批次信息
     * 
     * @param batchNo 批次号
     * @return 批次信息
     */
    @Override
    public Batch selectBatchByBatchNo(String batchNo)
    {
        return batchMapper.selectBatchByBatchNo(batchNo);
    }

    /**
     * 查询批次信息列表
     * 
     * @param batch 批次信息
     * @return 批次信息
     */
    @Override
    public List<Batch> selectBatchList(Batch batch)
    {
        return batchMapper.selectBatchList(batch);
    }
    
    /**
     * 批次追溯
     * 
     * @param batchCode 批次编号
     * @return 批次追溯信息
     */
    @Override
    public BatchTraceVO traceBatch(String batchCode)
    {
        BatchTraceVO traceVO = new BatchTraceVO();
        
        // 1. 获取批次基本信息
        Batch batch = batchMapper.selectBatchByBatchNo(batchCode);
        if (batch == null) {
            return null;
        }
        traceVO.setBatch(batch);
        traceVO.setProductName(batch.getProductName());
        
        // 2. 获取工序流转记录
        traceVO.setProcessRecords(processRecordMapper.selectProcessRecordByBatchId(batch.getId()));
        
        // TODO: 获取产品详细信息（规格、材质等）
        // TODO: 获取检验任务列表
        traceVO.setInspections(new ArrayList<InspectionSummary>());
        
        // TODO: 获取质量证书列表
        traceVO.setCertificates(new ArrayList<CertificateSummary>());
        
        return traceVO;
    }

    /**
     * 新增批次信息
     * 
     * @param batch 批次信息
     * @return 结果
     */
    @Override
    public int insertBatch(Batch batch)
    {
        batch.setCreateTime(DateUtils.getNowDate());
        return batchMapper.insertBatch(batch);
    }

    /**
     * 修改批次信息
     * 
     * @param batch 批次信息
     * @return 结果
     */
    @Override
    public int updateBatch(Batch batch)
    {
        batch.setUpdateTime(DateUtils.getNowDate());
        return batchMapper.updateBatch(batch);
    }

    /**
     * 批量删除批次信息
     * 
     * @param ids 需要删除的批次信息主键
     * @return 结果
     */
    @Override
    public int deleteBatchByIds(Long[] ids)
    {
        return batchMapper.deleteBatchByIds(ids);
    }

    /**
     * 删除批次信息信息
     * 
     * @param id 批次信息主键
     * @return 结果
     */
    @Override
    public int deleteBatchById(Long id)
    {
        return batchMapper.deleteBatchById(id);
    }

    /**
     * 根据产品ID获取批次列表
     *
     * @param productId 产品ID
     * @return 批次列表
     */
    @Override
    public List<Batch> getBatchesByProductId(Long productId) {
        Batch batch = new Batch();
        batch.setProductId(productId);
        return batchMapper.selectBatchList(batch);
    }
    
    /**
     * 根据产品名称获取批次列表
     *
     * @param productName 产品名称
     * @return 批次列表
     */
    @Override
    public List<Batch> getBatchesByProductName(String productName) {
        Batch batch = new Batch();
        batch.setProductName(productName);
        return batchMapper.selectBatchList(batch);
    }
} 